const http = require('http');

const appid= "wxd28c3700a03025da"
const secret= "d1ca50b4d81aa762372d5268aa61cff1"

function getToken(appid, secret, code, cb){
    // 用于请求的选项
    var options = {
        host: `api.weixin.qq.com`,
        port: '80',
        path: `/sns/oauth2/access_token?appid=${appid}&secret=${secret}&code=${code}&grant_type=authorization_code` 
     };
      
     // 处理响应的回调函数
     var callback = function(response){
        // 不断更新数据
        var body = '';
        response.on('data', function(data) {
           body += data;
        });
        
        response.on('end', function() {
           // 数据接收完成
           cb(body)
        });
     }
     // 向服务端发送请求
     var req = http.request(options, callback);
     req.end();
}

// router.get("/login",(req,res)=>{
//     console.log('body:',req.body)
//     console.log('query:',req.query)
//     if(req.query.code){
//         getToken(appid, secret, req.query.code,(data)=>{
//             console.log('data:',data)
//             res.send(data || "{name: ok}")
//         })
//     }
// })